package com.ysd.gulimall.product.controller;

import com.ysd.common.utils.PageUtils;
import com.ysd.common.utils.R;
import com.ysd.gulimall.product.entity.AttrEntity;
import com.ysd.gulimall.product.entity.AttrGroupEntity;
import com.ysd.gulimall.product.service.AttrAttrgroupRelationService;
import com.ysd.gulimall.product.service.AttrGroupService;
import com.ysd.gulimall.product.service.AttrService;
import com.ysd.gulimall.product.service.CategoryService;
import com.ysd.gulimall.product.vo.AttrGroupRelationVo;
import com.ysd.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author zhangjianzhong
 * @email ziziss@163.com
 * @date 2020-07-13 09:11:16
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    //注入三级分类业务层
    @Autowired
    private CategoryService categoryService;

    //关联功能操作使用了
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 添加属性与分组关联关系
     * /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 查询本分类下 没有被关联的属性
     * /product/attrgroup/{attrgroupId}/noattr/relation
     * 获取属性分组没有关联的其他属性
     * attrgroupId 分组id
     * params 分页参数
     */
    /**
     * 获取当前属性分组没有关联的其他属性
     * 注意：
     * 1、当前分组只能关联自己所属分类的属性
     * 1.2、获取当前分组的分类信息
     * 2.1、获取当前分类下的所有分组
     * 2、当前分组只能关联没有被引用的属性
     * 2.2.1 分组id集合
     * 3、分组只管关联基本属性不关联销售属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                           @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(attrgroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * /product/attrgroup/{attrgroupId}/attr/relation
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> data = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", data);
    }

    /**
     * 列表
     * 携带分类id：catelogId
     *
     * @PathVariable("catelogId") 标注当前变量为路径变量
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * /product/attrgroup/{catelogId}/withattr
     *
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {

        //1,查询分类下所有属性分组
        //2，查询属性分组下所有属性
        List<AttrGroupWithAttrsVo> data = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        /**
         * 根据分类id  获取详细路径
         */
        Long catelogId = attrGroup.getCatelogId();
        //调用分类业务
        //传入分类id  返回完整路径
        Long[] path = categoryService.findCatelogPath(catelogId);
        //设置
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
